/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.inteligencia.stage.web.eventos;

import com.inteligencia.stage.business.dto.AuditorioDTO;
import com.inteligencia.stage.business.dto.EventoDTO;
import com.inteligencia.stage.business.dto.PersonaDTO;
import com.inteligencia.stage.business.dto.TareaDTO;
import com.inteligencia.stage.dataaccess.domain.AuditorioDAO;
import com.inteligencia.stage.dataaccess.domain.AuditorioEventoDAO;
import com.inteligencia.stage.dataaccess.model.TaudiEveVO;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author igorov
 */
@ParentPackage(value = "stage")
public class ListarEventos extends ActionSupport {

    private List<EventoDTO> eventosUsuario;
    private List<AuditorioDTO> auditorioDTOs;
    private HttpSession session;
    private static final long serialVersionUID = 7968544374444173511L;
    private static final Log log = LogFactory.getLog(ListarEventos.class);
    private ApplicationContext ctx;

    @Action(value = "/jsonEventos", results = {
        @Result(type = "json", name = "success")
    })
    public String execute() {
        System.out.println("***jsonEventos");
        session = ServletActionContext.getRequest().getSession();
        PersonaDTO personaDTO = (PersonaDTO) session.getAttribute("usuario");

        List<EventoDTO> tmp = personaDTO.getEventoDTOs();
        for (Iterator<EventoDTO> it = tmp.iterator(); it.hasNext();) {
            EventoDTO eventoDTO = it.next();
            System.out.print("Evento: " + eventoDTO.getCodigo() + ", " + eventoDTO.getNomEve());
        }
        setEventosUsuario(tmp);

        return SUCCESS;
    }
    
    @Action(value = "/tablaAuditorios", results = {
        @Result(type = "json", name = "success")
    })
    public String tablaAuditorios() {
        ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        
        AuditorioEventoDAO auditorioEventoDAO = (AuditorioEventoDAO) ctx.getBean("auditorioEventoDAO");
        AuditorioDAO auditorioDAO = (AuditorioDAO) ctx.getBean("auditorioDAO");
        List<AuditorioDTO> auditorioDTOs = new LinkedList<AuditorioDTO>();
        
        System.out.println("***jsonAuditorios");
        session = ServletActionContext.getRequest().getSession();
        EventoDTO eventoDTO = (EventoDTO) session.getAttribute("evento");
//        PersonaDTO personaDTO = (PersonaDTO) session.getAttribute("usuario");
        
        List<TaudiEveVO> taudiEveVOs = auditorioEventoDAO.getByEvento(eventoDTO.getCodigo());
        for (Iterator<TaudiEveVO> it = taudiEveVOs.iterator(); it.hasNext();) {
            TaudiEveVO taudiEveVO = it.next();
            auditorioDTOs.add(new AuditorioDTO(auditorioDAO.get(taudiEveVO.getNcodaud())));
        }
        
        setAuditorioDTOs(auditorioDTOs);

        return SUCCESS;
    }

    @Action(value = "/verEventos", results = {
        @Result(location = "eventos/ver.jsp", name = "success")
    })
    public String verEventos() {
        System.out.println("***jsonEventos");
        session = ServletActionContext.getRequest().getSession();
        PersonaDTO personaDTO = (PersonaDTO) session.getAttribute("usuario");

        List<EventoDTO> tmp = personaDTO.getEventoDTOs();
        for (Iterator<EventoDTO> it = tmp.iterator(); it.hasNext();) {
            EventoDTO eventoDTO = it.next();
            System.out.print("Evento: " + eventoDTO.getCodigo() + ", " + eventoDTO.getNomEve());
        }
        setEventosUsuario(tmp);

        return SUCCESS;
    }

    public List<EventoDTO> getEventosUsuario() {
        return eventosUsuario;
    }

    public void setEventosUsuario(List<EventoDTO> eventosUsuario) {
        this.eventosUsuario = eventosUsuario;
    }

    public List<AuditorioDTO> getAuditorioDTOs() {
        return auditorioDTOs;
    }

    public void setAuditorioDTOs(List<AuditorioDTO> auditorioDTOs) {
        this.auditorioDTOs = auditorioDTOs;
    }
    
    
}
